<?php
require_once 'config21.php';
if (!isset($_SESSION['userid'])) {
    header('Location: login21.php');
    exit;
}

// 添加学生
if ($_SERVER["REQUEST_METHOD"] === "POST" && isset($_POST['add_student'])) {
    $student_name = $_POST['student_name'];
    $gender = $_POST['gender'];
    $birth_date = $_POST['birth_date'];
    $contact_info = $_POST['contact_info'];
    $class_id = $_POST['class_id'];
    
    $sql = "INSERT INTO students (student_name, gender, birth_date, contact_info, class_id) 
            VALUES ('$student_name', '$gender', '$birth_date', '$contact_info', '$class_id')";
    
    if ($conn->query($sql) === TRUE) {
        header('Location: student_management21.php');
        exit;
    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }
}
?>

<!DOCTYPE html>
<html lang="zh-Hans">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>添加学生</title>
    <link rel="stylesheet" href="style.css">
    <style>
        .add-button {
            display: inline-block;
            background-color: #007bff;
            color: white;
            padding: 8px 16px;
            text-decoration: none;
            border-radius: 4px;
        }
        .add-button:hover {
            background-color: #0056d3;
        }
    </style>
</head>
<body>
    <h2>添加学生</h2>
    <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">
        <label for="student_name">学生姓名：</label>
        <input type="text" name="student_name" required><br>

        <label for="gender">性别：</label>
        <input type="radio" name="gender" value="男" required>男
        <input type="radio" name="gender" value="女" required>女<br>

        <label for="birth_date">出生日期：</label>
        <input type="date" name="birth_date" required><br>

        <label for="contact_info">联系方式：</label>
        <input type="text" name="contact_info" required><br>

        <label for="class_id">所属班级：</label>
        <select name="class_id" required>
            <?php
            $class_sql = "SELECT * FROM classes";
            $class_result = $conn->query($class_sql);
            while ($class_row = $class_result->fetch_assoc()) {
                echo "<option value='" . $class_row['class_id'] . "'>" . $class_row['class_name'] . "</option>";
            }
            ?>
        </select><br>

        <input type="submit" value="添加" name="add_student">
    </form>
    <div align="right"><a href="student_management21.php" class="add-button">返回</a></div>
</body>
</html>